package test.core.fun;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.junit.Test;

import hyl.core.net.MyHttp;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.util.concurrent.TimeUnit;

public class testhttp {
	@Test
	public void testhttp3() throws IOException {

		HttpURLConnection conn = MyHttp.sendGet("https://www.baidu.com");
		// conn.connect();
		String s1 = MyHttp.getResult(conn);
		System.out.println(s1);
	}

	// @Test
	public void testhttp() throws IOException {
		HttpURLConnection conn = MyHttp.sendGet(
				"https://cn.etherscan.com/tx/0x3ec275c8ce9c785fd78f76a404d214fbcb1dbd8fffe0fcc30682941d54fde703");
		// conn.connect();
		String s1 = MyHttp.getResult(conn);
		String s;

		s = MyHttp.getHtmls(s1, "<span data-toggle=\"tooltip\" data-original-title=\"Current Price", ">", "</span");
		System.out.println(s);
		s = MyHttp.getTexts(s1, "<span data-toggle=\"tooltip\" data-original-title=\"Current Price", ">", "</span");
		System.out.println(s);
	}

//	@Test
	public void testhttp2() throws IOException {
		// HttpURLConnection conn=MyHttp.sendGet("http://www.baidu.com/");
		// String msg=MyHttp.getResult(conn);
		OkHttpClient client = new OkHttpClient.Builder().retryOnConnectionFailure(true)
				.connectTimeout(10, TimeUnit.SECONDS) // 连接超时
				.readTimeout(30, TimeUnit.SECONDS) // 读取超时
				.writeTimeout(10, TimeUnit.SECONDS) // 写超时
				.build();
		Request request = new Request.Builder().url("https://www.baidu.com/").build();
		Response response = client.newCall(request).execute();
		String msg = response.body().string();

		System.out.println(msg);
		int k = 10_000;
		System.out.println(k);
	}

	// @Test
	public void printip() throws SocketException {
		// System.out.println(MyFun.array2Str(Http.get外网IP(), ","));

		Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
		InetAddress ip = null;
		Inet4Address ip4 = null;
		ArrayList<InetAddress> ips = new ArrayList<>();
		while (allNetInterfaces.hasMoreElements()) {
			NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
			// System.out.println(netInterface.getName());
			Enumeration addresses = netInterface.getInetAddresses();

			while (addresses.hasMoreElements()) {
				ip = (InetAddress) addresses.nextElement();
				if (ip != null && ip instanceof Inet4Address && ip.isSiteLocalAddress()) {

					int n = NetworkInterface.getByInetAddress(ip).getInterfaceAddresses().get(0)
							.getNetworkPrefixLength();
					// System.out.println(ip.getCanonicalHostName()+"::"+ip.getHostAddress()+":"+s);

					System.out.println(n);
					ips.add(ip);

				}
			}
		}
		for (InetAddress s : ips) {
			try {

				System.out.println();

				NetworkInterface ni = NetworkInterface.getByInetAddress(s);// 搜索绑定了指定IP地址的网络接口
				List<InterfaceAddress> list = ni.getInterfaceAddresses();
				for (InterfaceAddress a : list) {
					System.out.println(a.getAddress());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		// 获取此网络接口的全部或部分
		// InterfaceAddresses

	}
}
